﻿using System;
using Spine.Unity;
using UnityEditor;
using UnityEngine;

namespace Learning.Spine
{
    /// <summary>
    /// UI 上展示的 Spine 加载和使用工具类
    /// </summary>
    public static class UISpineUtil
    {
        public static void LoadSpine(string spinePath, Transform parent, string animName, Action<GameObject> callback = null)
        {
            var asset = AssetDatabase.LoadAssetAtPath<GameObject>(spinePath);
            var go = GameObject.Instantiate(asset);
            
            go.transform.SetParent(parent, false);
            
            var skeletonGraphic = go.GetComponent<SkeletonGraphic>();
            skeletonGraphic.AnimationState.SetAnimation(0, animName, true);
            
            callback?.Invoke(go);
        }
    }
}
